<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="中文名称" prop="chineseName">
        <el-input v-model="queryParams.chineseName" placeholder="请输入中文名称"
          clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="英文名称" prop="englishName">
        <el-input v-model="queryParams.englishName" placeholder="请输入英文名称"
          clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择分类类型" clearable size="small">
          <el-option v-for="dict in $store.state.factorDict.typeOptions"
                     :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择分类级别" clearable size="small">
          <el-option v-for="dict in $store.state.factorDict.levelOptions"
                     :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
      <el-option v-for="dict in $store.state.factorDict.statusOptions"
                 :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
    </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini"
          @click="handleAdd" v-hasPermi="['factor:categorizeInfo:add']" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-refresh" size="mini" @click="handleClearCache"
                   v-hasPermi="['factor:categorizeInfo:clear']">清理缓存</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border lazy  :load="load"
      v-loading="loading"
      :data="categorizeInfoList"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="中文名称" align="left" prop="chineseName"  />
      <el-table-column label="英文名称" align="center" prop="englishName" />

      <el-table-column label="分类类型" align="center" prop="type" width="150">
        <template slot-scope="scope">
          {{selectDictLabelDefault($store.state.factorDict.typeOptions, scope.row.type)}}
        </template>
      </el-table-column>
      <el-table-column label="分类级别" align="center" prop="level" width="100">
        <template slot-scope="scope">
          {{selectDictLabelDefault($store.state.factorDict.levelOptions, scope.row.level)}}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum" width="100"/>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          {{selectDictLabelDefault($store.state.factorDict.statusOptions, scope.row.status)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['factor:categorizeInfo:edit']" >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['factor:categorizeInfo:remove']" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <change-form ref="changeFormRef" @getList="getList" :categorize-info-options="categorizeInfoOptions"/>

  </div>
</template>

<script>
import { listCategorizeInfo, getCategorizeInfo, delCategorizeInfo, addCategorizeInfo, updateCategorizeInfo, clearCategorizeInfoCache } from "@/api/factor/categorizeInfo";
import ChangeForm from "./changeForm"
export default {
  name: "CategorizeInfo",
  components:{
    ChangeForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 碳排放因子数据库表格数据
      categorizeInfoList: [],
      categorizeInfoOptions: [],
      // 查询参数
      queryParams: {
        parentId: null,
        chineseName: null,
        englishName: null,
        type: null,
        level: null,
        status: null,
        upstreamEmissions: null,
        upstreamUnit: null,
        downstreamEmissions: null,
        downstreamUnit: null,
        dataSource: null,
        dataSourceSimplified: null,
        principal: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询碳排放因子数据库列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.chineseName||this.queryParams.englishName||this.queryParams.type||this.queryParams.level||this.queryParams.status){
        this.queryParams.parentId = null
      }else {
        this.queryParams.parentId = 0;
      }
      listCategorizeInfo(this.queryParams).then(response => {
        // this.categorizeInfoList = this.handleTree(response.data, "id", "parentId");
        response.data.forEach((item, i) => {
          item.hasChildren = item['level']<3
        });
        this.categorizeInfoList = response.data;
        this.loading = false;
      });
    },
    load(tree, treeNode, resolve) {
      listCategorizeInfo({parentId: tree.id}).then(res => {
        res.data.forEach((item, i) => {
          item.hasChildren = item['level']<3
        });
        resolve(res.data)
      });
    },
    /** 转换碳排放因子数据库数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.chineseName,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listCategorizeInfo().then(response => {
        this.categorizeInfoOptions = [];
        const data = { id: 0, chineseName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data.filter(item => item.level && item.level<3), "id", "parentId");
        this.categorizeInfoOptions.push(data);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
	    this.getTreeselect();
      this.$refs.changeFormRef.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	    this.getTreeselect();
      this.$refs.changeFormRef.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCategorizeInfo(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    handleClearCache(){
      clearCategorizeInfoCache().then(response => {
        if (response.code === '200') {
          this.msgSuccess("清理成功");
        }
      });
    }
  }
};
</script>
